﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SPFileSize.ListItemSizeWebPart
{
    public static class Extensions
    {
        /// <summary>
        /// Returns a server relative url of the list
        /// </summary>
        /// <param name="typeToTarget"></param>
        /// <returns>string</returns>
        public static string Url(this SPList typeToTarget)
        {
            string listUrl = typeToTarget.DefaultViewUrl;
            if (typeToTarget is SPDocumentLibrary)
            {

                int index = listUrl.IndexOf("Forms") + 6;
                listUrl = listUrl.Remove(index);

                if (listUrl.EndsWith("/"))
                {
                    listUrl = listUrl.Remove(listUrl.LastIndexOf("/"));
                }
            }
            else
            {
                int indexSlash = listUrl.LastIndexOf("/");
                listUrl = listUrl.Remove(indexSlash);
            }
            return listUrl;
        }
    } 


}
